using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Mvc.Validation;
using web_uchet.Security.Enums;
using web_uchet.Security.Interfaces;
using web_uchet.ViewModels;
using web_uchet.Security;

namespace web_uchet.Controllers
{
    public class ViewUserController : MasterPageController
    {
        private ViewПользовательOperationProvider _Provider;
        public ViewUserController()
        {
            this._Provider = new ViewПользовательOperationProvider();
        }
        public ViewUserController(ViewПользовательOperationProvider Provider)
        {
            this._Provider = Provider;
        }
        //
        // GET: /ViewПользователь/
        [Authorize(Roles = "Администратор,Просмотр")]
        public ActionResult Index()
        {
            ProgramContext MPageData = this.ViewData["Master"] as ProgramContext;
            return View(this._Provider.List(MPageData.CompanyID));
        }

        //
        // GET: /ViewПользователь/Details/5
        [Authorize(Roles = "Администратор,Просмотр")]
        public ActionResult Details(int id)
        {
            return View(this._Provider.Details(id, this.ViewData["Master"] as ProgramContext));            
        }

        //
        // GET: /ViewПользователь/Create

        //public ActionResult Create()
        //{
        //    return View();
        //} 

        ////
        //// POST: /ViewПользователь/Create

        //[AcceptVerbs(HttpVerbs.Post)]
        //public ActionResult Create(FormCollection collection)
        //{
        //    try
        //    {
        //        

        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}

        //
        // GET: /ViewПользователь/Edit/5
        [Authorize(Roles = "Администратор")]
        public ActionResult Edit(int id)
        {
            var EditObject = this._Provider.Details(id, this.ViewData["Master"] as ProgramContext);
            ViewData.Add("ИД_Место_Работы", 
                this._UserOperationProvider.GetCurrentCompanyWorkPlacesList(new int?(EditObject.ИД_Место_Работы)));
            ViewData.Add("Role1", this._UserOperationProvider.GetUserRolesList(EditObject.Role1));
            ViewData.Add("Role2", this._UserOperationProvider.GetUserRolesList(EditObject.Role2));
            ViewData.Add("Role3", this._UserOperationProvider.GetUserRolesList(EditObject.Role3));
            return View(EditObject);
        }

        //
        // POST: /ViewПользователь/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize(Roles = "Администратор")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            ViewПользователь EditObject = this._Provider.Details(id, this.ViewData["Master"] as ProgramContext);
            try
            {   
                UpdateModel<ViewПользователь>(EditObject,collection.ToValueProvider());
                EditObject.Validate();
                //this._Provider.SaveChanges();
                this._Provider.Update(EditObject);
                return RedirectToAction("Index");
            }
            catch(EntityValidationException ex)
            {
                ViewData.Add("ИД_Место_Работы", this._UserOperationProvider.GetCurrentCompanyWorkPlacesList(EditObject.ИД_Место_Работы));
                ViewData.Add("Role1", this._UserOperationProvider.GetUserRolesList(EditObject.Role1));
                ViewData.Add("Role2", this._UserOperationProvider.GetUserRolesList(EditObject.Role2));
                ViewData.Add("Role3", this._UserOperationProvider.GetUserRolesList(EditObject.Role3));
                ViewData.ModelState.PopulateWithErrors(ex);
                return View(EditObject);
            }
        }
        [Authorize(Roles = "Администратор")]
        public string Delete(int id)
        {
            if (Request.IsAjaxRequest())
            {
                return this._Provider.Delete(id, this.ViewData["Master"] as ProgramContext);
            }
            else return string.Empty;
        }
    }
}
